package com.ruoyi.wx.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ScoreUtile;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.wx.entity.PtExpClass;
import com.ruoyi.wx.entity.PtReport;
import com.ruoyi.wx.entity.PtScore;
import com.ruoyi.wx.service.IPtExpClassService;
import com.ruoyi.wx.service.IPtReportService;
import com.ruoyi.wx.service.IPtScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@RestController
@Api(value = "实验报告接口", tags = {"实验报告接口测试"})
@RequestMapping("/wx")
public class PtReportController {
    @Autowired
    private IPtReportService iPtReportService;

    @ApiOperation("实验报告上传接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = File.class, required = true),
    })
    @PostMapping("/uploadReport")
    public AjaxResult uploadReport(@RequestPart("file") MultipartFile file,Long expId) {
        if (file == null) {
            return AjaxResult.error("实验报告不能为空");
        }else {
            String originalfileName = file.getOriginalFilename();
            String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
            if (!".pdf".equals(suffix)) {
                return AjaxResult.error("实验报告类型为pdf");
            }
            iPtReportService.uploadReport(file, expId);
            return AjaxResult.success();
        }
    }

    @GetMapping("/getReportPdf")
    @ApiOperation("获取实验报告pdf")
    public AjaxResult getReportPdf(Long expId) {
        return AjaxResult.success(iPtReportService.getOne(Wrappers.lambdaQuery(PtReport.class)
            .eq(PtReport::getExpId,expId).eq(PtReport::getUserId,SecurityUtils.getUserId())
            .select(PtReport::getSubmitTime,PtReport::getSubmitUrl)
        ));
    }



}
